----------------------------------------------
-- lolita's minigame-daily function V 1.6.1 --
-- 		updated to 1.7 bliblab lyliaya	    --
--		Game Version: 6.0.4.2677			--
----------------------------------------------
					----------------------
					--== User Options ==--
					----------------------
Junk_Resiver = "Smecar" 	-- Change name of character for "Junk" items from Curse of Terror
Goods_Resiver = "Medic" 	-- Change name of character for "Good" items from Curse of Terror
Fusier = "Toxaris"			-- Change name of character for fusing mana stones to higher grade
Stat_Resiver = "Hulala"		-- Change name of character for colecting stat buyed from shels
Gel_Resiver = "Medic"		-- Change name of character for colecting Gel's for lv 60 elite skill

--	List of Stat ID's wich will be stoned
--	add more with same method
local goodstats = {
		-----------------
		-- 80 dex mods --
		-----------------
			--513652, -- "Elemental Swiftness"		-- 150Dex/975Hp
			--513653, -- "Elemental Triumph"		-- 150Dex/375Patt
			--513655, -- "Elemental Transformation"	-- 375Patt/825Pdef
		---------------------
		-- 82 stamina mods --
		---------------------
			513846, -- "Guardian of Disorder" 		-- 156Stam/390Patt
			513847, -- "Guard of Disorder" 			-- 156Stam/156Dex
			513848, -- "Protection of Disorder" 	-- 156Stam/858Pdef
		------------------
		--82 intel mods --
		------------------
			513859, -- "Spell of Disorder"			-- 156Int/156Stam
			513861, -- "Illusion of Disorder"		-- 156Int/390Matt
		---------------------
		-- 85 stamina mods --
		---------------------
			513930, -- "Guard of ???" 			-- 165Stam/401Patt
			513931, -- "Guard of ???" 			-- 165Stam/165Dex
			513932, -- "Protection of ???" 		-- 165Stam/907Pdef
		------------------
		--85 intel mods --
		------------------
			513943, -- "Spell of ???"			-- 165Int/165Stam
			513945, -- "Illusion of ???"		-- 165Int/412Matt
		---------------------
		-- 87 stamina mods --
		---------------------
			514003, -- "Guardian of the Jungle" 	-- 171Stam/427Patt
			514004, -- "Guard of the Jungle" 		-- 171Stam/171Dex
			514005, -- "Protection of the Jungle" 	-- 171Stam/940Pdef
		------------------
		--87 intel mods --
		------------------
			--513943, -- "Spell of the Jungle"			-- 171Int/171Stam
			--513945, -- "Illusion of the Jungle"		-- 171Int/427Matt
		}
	
--	List of items wich will always be droped
--	add more with same method)
force_drop = {
	"Curse Scroll (Cannon Permutation)",
	"Curse Scroll (Instant Transport)",
	"Curse Scroll (Wicked Freeze)",
	"Curse Scroll (Time Extension)",
	"Vanquisher's Loot",
	"Royal Furniture Recipe Collection Box",
	"Potion: Princely Look",
	"Potion: Fullness",
	"Phirius Potion - Type A",
	"Phirius Potion - Type B",
	"Phirius Elixir - Type A",
	"Phirius Elixir - Type B",
	"Phirius Special Water - Type A",
	"Phirius Special Water - Type B",
	"Simple Repair Hammer",
	"Horse Rental Ticket",
	   }
-- List of item's witch will be sended to Junk_Resiver
CoT_Junk = {
	--201967, 	-- Simple Repair Hammer
	203028,		-- Craftsman's Recipe Coupon
	--203033,		-- Horse Rental Ticket
	203635,		-- Material Package for Magical Instruments
	205821,		-- Temporary Mount Use Voucher
}
-- List of item's witch will be sended to Goods_Resiver
CoT_Good = {
	205820,		-- Magic Instrument Collection Box
	--205817,		-- Vanquisher's Loot
	--"Royal Furniture Recipe Collection Box",
}
					-----------------------------
					--== End of User Options ==--
			 ----------------------------------------------
			--== dont change anything behinde this line ==--
			 ----------------------------------------------
-- Globals


logentry = nil
Malatina = GetIdName(111813)

-- list of tier 5 items
  Tier5_list = {
	-- KBN items
	227700,	227701,	227702,	227703,	227704,
	227705,	227706, 227707, 227708, 227709,
	227710,	227711,
	-- Bethomia items
	213783,	213791,	213794,	213795,	213796,
	213797,	213798,	213799,	213800,	213801,
	213802,	213803,	213804,	213805, 213806,
	213807, 213808, 213809, 213810, 213811,
	213812, 213813, 213814, 213815, 230696,
	230690, 230697, 230698, 230699, 230700,
	230701, 230702, 230703, 230704, 230705,
	230706, 230707, 230708, 230709, 230710,
	230711, 230712, 230713, 230714, 230715,
	230716, 230717, 230718, 230719, 230720,
	-- Other items
	231921, 231923, 231965,
}
-- welcome note
function lolitaswelcomemsg()
	local pfs,cot,lili,gel,stone
	if Take_PFS then pfs = "Yes" else pfs = "No" end
	if Do_CoT then cot = "Yes" else cot = "No" end
	if Do_Lylyia then lili = "Yes" else lili = "No" end
	if Colect_Gel then gel = "Yes" else gel = "No" end
	if Stone_Stats then stone = "Yes" else stone = "No" end
	cprintf(cli.pink,"     #####################################\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue,"		Hello")										cprintf(cli.pink,"			##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow,"	Welcome to lolita's")							cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue,"	Minigame Daily waypoint")						cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     #####################################\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.green," User options are set to: ")							cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," When Finished = "..When_Finished)				cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Do CoT Minigame = "..cot)							cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Do Lylyia Minigame  = "..lili)					cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Colect gels (elite LV70) = "..gel)					cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Save Phirius tokens = "..SaveTokens)			cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Use Phirius Shels = +"..Shell_Num)					cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Buy item's with = "..Use_Shels)					cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Take reward =  "..pfs)								cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Auto stone stats = "..stone)					cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Curent reward level =  "..Curen_Reward_LV)			cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Wanted level is = "..Wanted_LV)					cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Chose mailbox = "..Chose_Mailbox)					cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," What to do with recipe's = "..Recept)			cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Restar client after =  "..Restart_After.." chars")	cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Quit game after lastchar = "..Quit_Game)		cprintf(cli.pink," ##\n")
	cprintf(cli.pink,"     #####################################\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.green," Mail recipient list:")								cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Curse of Terror junk = "..Junk_Resiver)			cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Curse of Terror good's = "..Goods_Resiver)			cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Tier stone resiver = "..Fusier)					cprintf(cli.pink,"	##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.yellow," Stat resiver =  "..Stat_Resiver)					cprintf(cli.pink,"		##\n")
	cprintf(cli.pink,"     ##")										cprintf(cli.lightblue," Lv 70 elite gel resiver = "..Gel_Resiver)		cprintf(cli.pink," ##\n")
	cprintf(cli.pink,"     #####################################\n")
end
-- Spend shell's function
function AtStore()
	if (Use_Shels == "stam") then
		cprintf(cli.yellow,"We gonna buy item with Stamina.\n")
		player:target_NPC("Byron - Archeologist")
		sendMacro("ChoiceOption(1);"); yrest(2000)
		RoMScript("StoreBuyItem(7, 1)") -- Plate belt
	elseif (Use_Shels == "str") then
		cprintf(cli.yellow,"We gonna buy item with Strenght.\n")
		player:target_NPC("Joel - Archeologist")
		sendMacro("ChoiceOption(1);"); yrest(2000)
		RoMScript("StoreBuyItem(7, 1)") -- Chain belt
	elseif (Use_Shels == "dex") then
		cprintf(cli.yellow,"We gonna buy item with Dexterity.\n")
		player:target_NPC("York - Archeologist")
		sendMacro("ChoiceOption(1);"); yrest(2000)
		RoMScript("StoreBuyItem(7, 1)") -- Leather belt
	elseif (Use_Shels == "int") then
		cprintf(cli.yellow,"We gonna buy item with Intelligence.\n")
		player:target_NPC("Yona - Archeologist")
		sendMacro("ChoiceOption(1);"); yrest(2000)
		RoMScript("StoreBuyItem(7, 1)") -- Mage belt
	elseif (Use_Shels == "wis") then
		cprintf(cli.yellow,"We gonna buy item with Wisdom.\n")
		player:target_NPC("Wendy - Archeologist")
		sendMacro("ChoiceOption(1);"); yrest(2000)
		RoMScript("StoreBuyItem(7, 1)") -- Healer belt
	else
		cprintf(cli.yellow,"Wrong option. Going to sleep.\n")
		player:sleep()
	end
	-- we need to change this
	if Stone_Stats then
		yrest(3000)
		StoneIt()
	end
	player:moveTo(CWaypoint(4771,-2099,121))
	DoCoT()
end
-- Use shell function
function UseShells()
	local numshells = getCurrency("shell")
	if numshells > 950 then
		if (Use_Shels == "false") then
			cprintf(cli.yellow,"Reaching the maximum number of Phirius Shells (1000). Please spend some before continuing.\n")
			player:sleep()
		else
			player:moveTo(CWaypoint(4771,-2099,121))
			player:moveTo(CWaypoint(4678,-2379,103))
			AtStore()
		end
	elseif numshells > Shell_Num then
		cprintf(cli.yellow,"We have "..numshells.." Shells. Use Shell is set to "..Shell_Num.."+.\n")
		player:moveTo(CWaypoint(4771,-2099,121))
		player:moveTo(CWaypoint(4678,-2379,103))
		AtStore()
	end
end
-- Colect Gel for elite skill
function ColectGel()
	if Colect_Gel then
		RoMScript("UseEquipmentItem(18)")
		yrest(1500)
		RoMScript("UseEquipmentItem(19)")
		yrest(1500)
		RoMScript("UseEquipmentItem(20)")
		yrest(1500)
		local apple = inventory:findItem(240773)
		local pearl = inventory:findItem(240772)
		if apple then
			local empties = inventory:itemTotalCount(0)
			cprintf(cli.yellow,"'Colect_Gel' is set to true.\n This char colecting Apple Gel's.\n")
			player:moveTo(CWaypoint(5207,-2146,103))
			UMM_SendByNameOrId(Gel_Resiver,240773);
			if Stone_Stats then
				if Stat_Resiver ~= player.Name then
					UMM_SendByNameOrId(Stat_Resiver, 202844)
				end
			end
			if 10 > empties then
				cotclean()
			end
			if ((player.Level >= Curen_Reward_LV) and Take_PFS) then
				cprintf(cli.yellow,"We going to take reward.\n")
				player:moveTo(CWaypoint(5228,-2057,121))
				--player:target_NPC("Tina"); yrest(500)
				--sendMacro("ChoiceOption(1);")
				player:target_NPC(115809); yrest(500)
				sendMacro("ChoiceOption(1);")		
				moveitemshop()
				Take_PFS = false
				player:moveTo(CWaypoint(4871,-1885,103))
				SaveToken()
			else
				player:moveTo(CWaypoint(4871,-1885,103))
				SaveToken()
			end
		elseif pearl then
			local empties = inventory:itemTotalCount(0)
			cprintf(cli.yellow,"'Colect_Gel' is set to true.\n This char colecting Snowpearl Gel's.\n")
			player:moveTo(CWaypoint(5207,-2146,103))
			UMM_SendByNameOrId(Gel_Resiver,240772);
			if Stone_Stats then
				if Stat_Resiver ~= player.Name then
					UMM_SendByNameOrId(Stat_Resiver, 202844)
				end
			end
			if 10 > empties then
				cotclean()
			end
			if ((player.Level >= Curen_Reward_LV) and Take_PFS) then
				cprintf(cli.yellow,"We going to take reward.\n")
				player:moveTo(CWaypoint(5228,-2057,121))
				--player:target_NPC("Tina"); yrest(500)
				--sendMacro("ChoiceOption(1);")
				player:target_NPC(115809); yrest(500)
				sendMacro("ChoiceOption(1);")		
				moveitemshop()
				Take_PFS = false
				player:moveTo(CWaypoint(4871,-1885,103))
				SaveToken()
			else
				player:moveTo(CWaypoint(4871,-1885,103))
				SaveToken()
			end
		else
			SaveToken()
		end
	else
		SaveToken()
	end
end
-- Take reward
function TakePfs()
	if Take_PFS then
		if  player.Level >= Curen_Reward_LV then
			cprintf(cli.yellow,"We going to take reward.\n")
			player:moveTo(CWaypoint(5228,-2057,121))
			--player:target_NPC("Tina"); yrest(500)
			--sendMacro("ChoiceOption(1);")
			player:target_NPC(115809); yrest(500)
			sendMacro("ChoiceOption(1);")		
			moveitemshop()
			DoCoT()
		else
			DoCoT()
		end
	else
		DoCoT()
	end
end
-- go to left mail
function LeftMail()
	player:moveTo(CWaypoint(5207,-2146,103))
	cotclean()
end
-- go to right mail
function RightMail()
	player:moveTo(CWaypoint(4656,-2008,109))
	player:moveTo(CWaypoint(4573,-2183,103))
	cotclean()
end
-- check bag space function
function CheckBagSpace()
	local empties = inventory:itemTotalCount(0)
	if 10 > empties then
		cprintf(cli.yellow,"Not much space left in inventory. We are going to clear bag.\n")
		if Chose_Mailbox == "left" then
			LeftMail()
			TakePfs()
		elseif Chose_Mailbox == "right" then
			if Take_PFS then
				LeftMail()
				TakePfs()
			else
				RightMail()
				TakePfs()
			end
		end
		if 10 > inventory:itemTotalCount(0) then
			print("something got wrong")
			player:sleep()
		end
	elseif Take_PFS then
		TakePfs()
	else
		DoCoT()
	end
end
-- Drop items function
function ForceDrop()
	for i, item in pairs(inventory.BagSlot) do
		if item.SlotNumber >= settings.profile.options.INV_AUTOSELL_FROMSLOT + 60 and
		settings.profile.options.INV_AUTOSELL_TOSLOT + 60 >= item.SlotNumber then
			for k, v in ipairs(force_drop) do
				if v == item.Name then
					printf("Deleting Item:  "..item.Name.."\n");
					item:delete();
				end
			end
		end
	end
end
-- learn recipe's
function nauci()
	for i, item in pairs(inventory.BagSlot) do
		if item:isType("Recipes") then
			if RoMScript("GetCraftItemInfo("..item.Id..")") == nil then -- Don't have it
				printf("Learning recipe:  "..item.Name.."\n");
				item:use();
				yrest(5000);
			else
				printf("Deleting Recipe:  "..item.Name.."\n");
				item:delete();
			end
		end
	end
end
-- Deal with Recipe's
function Recepti()
	if Recept == "send" then
		UMM_SendAdvanced(Goods_Resiver, nil, nil, nil, nil, "Recipes")
	elseif Recept == "drop"	then
		for i, item in pairs(inventory.BagSlot) do
			if item:isType("Recipes") then
				printf("Deleting Recipe:  "..item.Name.."\n")
				item:delete();
			end
		end
	else
		error("Wrong 'Recept' option specified",0)
	end
end
-- Curse of terror clean bag function
function cotclean()
	player:target_Object ("Mailbox")	yrest(1000);
	if Junk_Resiver ~= player.Name then
		UMM_SendByNameOrId(Junk_Resiver,CoT_Junk);
	end
	if Goods_Resiver ~= player.Name then
		UMM_SendByNameOrId(Goods_Resiver,CoT_Good);
		Recepti()
	end
	if Stat_Resiver ~= player.Name then
		UMM_SendByNameOrId(Stat_Resiver, 202844)
	end
	RoMScript("UMMFrame:Hide()")	yrest(1000);
	if inventory:itemTotalCount("Arcane Transmutor Charge") > 0 then
		repeat
			inventory:useItem("Arcane Transmutor Charge")
		until inventory:itemTotalCount("Arcane Transmutor Charge") == 0
	end
	if inventory:itemTotalCount("Witchcraft Scroll Collection Box") > 0 then
		repeat
			inventory:useItem("Witchcraft Scroll Collection Box")
		until inventory:itemTotalCount("Witchcraft Scroll Collection Box") == 0
	end
	inventory:update()
	ForceDrop()
end
-- Count inventory total item count of table
function CountTable(_table)
    local function isInTable(_id) -- Function to check if id is in table
        for k, id in pairs(_table) do
            if id == _id then return true end
        end
        return false
    end

    local first, last = 61, 240 -- Slot number range
    local item
    local count = 0
    for slot = first, last do
        item = inventory.BagSlot[slot]
        item:update()
        if item.Available and (not item.Empty) and isInTable(item.Id) then
            count = count + item.ItemCount
        end
    end
    return count
end
-- Read file and send items to chars
function SendItemsFromFile()
	local lines = {}
	local filename = getExecutionPath() .. "/logs/SendItems.txt"
	local file, err = io.open(filename, "r");
	if( not file ) then
		print("File not found");
		player:sleep()
	else
		local newlines = {}
		-- read the lines in table 'lines'
		for line in io.lines(filename) do
		  table.insert(lines, line)
		end
		-- sort
		table.sort(lines)
		table.concat(lines, "\n")
		cprintf(cli.blue,"We need To send to those chars:\n")
		for k,v in pairs(lines) do
			print(k,v)
		end
		cprintf(cli.green,"Start Sending Items\n")
		-- send items
		for i, l in ipairs(lines) do
			-- read the lines in table 'newlines'
			if newlines[1] == nil then
				cprintf(cli.green,"Reading lines...\n")
				for line in io.lines(filename) do
					table.insert(newlines, line)
					cprintf(cli.lightblue,"Adding "..line.." to list\n")
				end
				table.sort(newlines)
			end
			if (CountTable(Tier5_list) >= 9) then
				UMM_SendByNameOrId(l, Tier5_list, 9)
				logInfo("Tiering Log", l.." got 9 items.",true)
				table.remove(newlines, 1)
			else
				RoMScript("UMMFrame:Hide()")
				UMM_TakeMail()
				if (CountTable(Tier5_list) >= 9) then
					UMM_SendByNameOrId(l, Tier5_list, 9)
					logInfo("Tiering Log", l.." got 9 items.",true)
					table.remove(newlines, 1)
				else
					cprintf(cli.red,"Not enof T5 items for "..l..".\n")
					logInfo("Tiering Log", l.." didn't got Items.",true)
				end
			end
		end
		file:close()
		local newfile, err = io.open(filename, "w+");
		newfile:write(table.concat(newlines, "\n"))
		yrest(1000)
		newfile:close()
		cprintf(cli.green,"All done\n")
	end
end
-- makeing T5 stats from minigame
function tierpet()
	local fuse_list = {
		"Volcanic Subversion Belt",	-- stamina lv 80 item
		"Volcanic Sacrifice Belt",	-- wisdom lv 80 item
		"Volcanic Wisdom Belt",		-- intel lv 80 item
		"Volcanic Vengeance Belt",	-- dexterity lv 80 item
		"Volcanic Crush Belt"		-- strenght lv 80 item  
	}
	
	for k,v in pairs(fuse_list) do
		repeat
			local item = inventory:findItem(v,"bags")
			local PFS = inventory:findItem("Purified Fusion Stone")
			if item then
				EmptyMagicbox(quick)
				if PFS then
					item:moveTo("magicbox")
					yrest(1000)
					PFS:moveTo("magicbox")
					yrest(1000)
					RoMScript("MagicBoxFrameOkayButton_OnClick()")
					yrest(3000)
					EmptyMagicbox(quick)
					yrest(1000)
				else
					printf("We dont have Puried Fusion Stone.\n");
				end
			end
		until not item or inventory:itemTotalCount("Purified Fusion Stone") == 0
	end
end
-- Moveing items to items shop function
function moveitemshop()
	yrest(500)
	inventory:update()
	local item_shop_bag = {
	   "Purified Fusion Stone",
	   "Master's Simple Repair Hammer",
	   "Grand Golden Repair Hammer",
	   }

	for k,v in pairs(item_shop_bag) do
		repeat
			local item = inventory:findItem(v,"bags")
			if item then
				printf("Moving to Item Shop bag >>> "..item.Name.." <<<\n");
				item:moveTo("itemshop")
			end
		until not item or inventory:itemTotalCount(0,"itemshop") == 0
	end
end
-- Empty transmutor function
function EmptyMagicbox(quick)
	local function findEmpty(num)
		local count = 0
		for i = 61,240 do
			local item = inventory.BagSlot[i]
			item:update()
			if item.Available and item.Empty then
				count = count + 1
			end
			if count >= num then
				return item
			end
		end
	end

	if quick then
		local emptynum = 0
		local itemmoved = false
		for i = 51,60 do
			local item = inventory.BagSlot[i]
			item:update()
			if not item.Empty then
				emptynum = emptynum + 1
				local BagEmpty = findEmpty(emptynum)
				if not BagEmpty then
					error("Not enough bag space to empty transmutor.",0)
				end
				item:pickup()
				BagEmpty:pickup()
				itemmoved = true
			end
		end
		if itemmoved then
			yrest(500)
		end
	else
		for i = 51,60 do
			local item = inventory.BagSlot[i]
			item:update()
			if not item.Empty then
				item:moveTo("bags")
			end
		end
	end
end
--============================--
-- Rock5 Curse of Terror code --
--============================--
function checkaggro_L()
	repeat
		player:update()
		if player.Battling then
			local clown = player:findEnemy(true)
			player.X = clown.X player.Z = clown.Z -- Temporarily change player coords so we can find nearest seal to mob
			local seal = player:findNearestNameOrId(102384)
			player:target(seal) -- kills the summoning portal and not the ghost clown.
			player:fight()
			yrest(2000) -- give a couple of seconds for the clown to disappear.
			player:update()
		end
	until not player.Battling
end

function getallkeys_L()
	local lastSealAddress = 0
	repeat
		player:update()
		local seal = player:findNearestNameOrId(113112, lastSealAddress)
		if seal then
			-- goto seal
			teleport(nil,nil,73)            -- go up to height if 73
			teleport(seal.X, seal.Z, 73)	-- teleport to above seal
			teleport(seal.X + 1, seal.Z, 57)	-- teleport in range to click seal

			-- click seal
			repeat
				checkaggro_L()
				player:target(seal)
				yrest(100)
				Attack()
				yrest(1000)
				player:update()
			until player:findNearestNameOrId(113106) -- until a key appears

			-- Remember last seal
			lastSealAddress = seal.Address

			-- Collect key
			local keys = inventory:itemTotalCount(203027)

			repeat
				checkaggro_L()
				player:target_Object(113106,nil,nil,true) -- harvests key
			until inventory:itemTotalCount(203027) == keys + 1
			yrest(500)

		end
	until gotAllKeys_L()
end

function gotogate_L()
	teleport(nil,nil,73) 			-- go up to height if 73
	teleport(4080,3948.5,73) 		-- teleport to above portal
	teleport(4080,3974,30) 		-- teleport into portal
	player:faceDirection(3.14/2) -- Face forward
	keyboardPress( settings.hotkeys.MOVE_FORWARD.key );

	repeat
		player:update()
		dist = distance(player.X, player.Z, 4081, 3335)
		yrest(700)
	until 100 > dist
	yrest(500)
	player:update()
	if 18 > player.Y then
		print("\a")
		local y = player.Y
		for i = 1,20 do teleport(nil,nil,y+i) player:turnDirection(1) yrest(50) end
		keyboardPress( settings.hotkeys.MOVE_FORWARD.key ); yrest(500)
		player:update()
		if 18 > player.Y then
			print("\a\aplayer is stuck")
			player:sleep()
		end
	end

	printf("Distance checked and continuing.\n")
end

function gotAllKeys_L()
	local room = currentRoom_L()
	local keys = inventory:itemTotalCount(203027)
	local keyGoal = (room == 1 and 1) or (room == 2 and 3) or (room == 3 and 6) or (room == 4 and 10)
	return keys >= keyGoal
end
	
function currentRoom_L()
	return memoryReadRepeat("int",getProc(), player.Address + 0x1cc)
end
	
function enterCoT_L()
	if Do_CoT then
		changeProfileOption("HARVEST_DISTANCE", 450);
		local zoneid = RoMScript("GetZoneID()")
		if zoneid ~= 353 then -- not in Course of Terror
		
			UseShells()
			
			if 30 > inventory:itemTotalCount(203038) then
				print("Not enough Phirius Token Coins.")
				logentry = "Not enough Phirius Token Coins."
				checkRelog()
				return
			end

			-- Try to find Malatina and teleport in
			if player:target_NPC(Malatina) then
				yrest(1000)
				
				if 2 > RoMScript("GetNumSpeakOption()") then -- try again
					player:target_NPC(Malatina) yrest(1000)
				end

				sendMacro("ChoiceOption(2);");
				yrest(2000)

				if RoMScript("GetNumSpeakOption()") == 3 then -- enter game
					sendMacro("ChoiceOption(1);");
					cprintf(cli.yellow,"Entering Curse of Terror now.\n")
					if not waitForLoadingScreen(30) then
						print("Failed to teleport into Course of Terror")
						logentry = "Failed to teleport into Course of Terror."
						checkRelog()
					end
					yrest(1000)
					__WPL:setWaypointIndex(__WPL:getNearestWaypoint(player.X,player.Z))
				else
					print("This character already did Course of Terror.")
					logentry = "Already did Course of Terror. Going to survival game."
					sendMacro("ChoiceOption(1);");
					yrest(2000)
					sendMacro("ChoiceOption(1);");
					yrest(2000)
					sendMacro("ChoiceOption(1);");
					cprintf(cli.yellow,"Entering Survival game now.\n")
					waitForLoadingScreen(30)
					checkRelog()
				end
			else
				print("You are too far from Malatina")
				logentry = "Too far from Malatina."
				checkRelog()
			end
		end
	elseif (Do_CoT == false) then
		cprintf(cli.yellow,"We wont do Curse of Terror, going to end.\n")
		player:moveTo(CWaypoint(4969,-1825,103))
		checkRelog()
	else
		error("Wrong Do_CoT option specified",0)
	end
end

function StoneIt()
	local itemtable = {
	-- Lv 80 belt item's
		--"Volcanic Subversion Belt",	-- stamina lv 80 item
		--"Volcanic Sacrifice Belt",	-- wisdom lv 80 item
		--"Volcanic Wisdom Belt",		-- intel lv 80 item
		--"Volcanic Vengeance Belt",	-- dexterity lv 80 item
		--"Volcanic Crush Belt",		-- strenght lv 80 item
	-- Lv 82 sholder's
		231316,						-- stamina lv 82 item
		--231320,						-- strenght lv 82 item (no stat id's)
		--231324,						-- dexterity lv 82 item (no stat id's)
		231328,						-- intel lv 82 item
		--231332,						-- wisdom (no stat id's)
	-- Lv 85 belt's
		231395,		-- stamina
		--231399,		-- strenght (no stat id's)
		--231403,		-- dexterity (no stat id's)
		231407,		-- intel
		--231411,		-- wisdom (no stat id's)
	-- Lv 87 Sholders's
		232671,		-- stamina
		--231399,		-- strenght (no stat id's)
		--231403,		-- dexterity (no stat id's)
		--231407,		-- intel
		--231411,		-- wisdom (no stat id's)
		}
	for k, v in ipairs(itemtable) do
		repeat
			yrest(500)
			cprintf(cli.yellow,"Searching for '"..v.."'\n")
			local item = inventory:findItem(v,"bags")
			local PFS = inventory:findItem("Purified Fusion Stone")
			local sell_it
			if not PFS then
				print("No pfs")
				break
			end
			if item then
				EmptyMagicbox(quick)
				for k, v in pairs(goodstats) do
					local statid = item.Stats[1].Id
					print(v)
					print("item hase stat "..statid)
					if statid == v then
						cprintf(cli.green,"Bingo\n")
						if PFS then
							print("we got a match "..statid.." we will stone item")
							item:moveTo("magicbox")
							yrest(1000)
							PFS:moveTo("magicbox")
							yrest(1000)
							RoMScript("MagicBoxFrameOkayButton_OnClick()")
							yrest(3000)
							EmptyMagicbox(quick)
							yrest(1000)
							print("Stated")
						else
							printf("We dont have Puried Fusion Stone.\n");
						end
						sell_it = false
						break
					else
						sell_it = true
					end
				end
				if sell_it then
					print("we will sell "..v)
					item:use()
				end
			end
			cprintf(cli.lightblue,"Next item\n")
		until not item or not PFS
	end
	cprintf(cli.green,"Finished\n")
	-- if we stoned stat, send it
	if inventory:itemTotalCount(202844) > 0 then
		inventory:update()
		player:moveTo(CWaypoint(4632,-2236,103))
		player:target_Object("Mailbox")	yrest(1000);
		if Stat_Resiver ~= player.Name then
			UMM_SendByNameOrId(Stat_Resiver, 202844)
		end
		-- Check for PFS
		if inventory:itemTotalCount(202881) < 1 then
			if Goods_Resiver ~= player.Name then
				UMM_SendMoney(Goods_Resiver, 1)
			end
		end
		player:moveTo(CWaypoint(4632,-2236,103))
	end
end
